package com.hanson.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.exception.BusinessException;
import com.hanson.common.util.BusinessUtil;
import com.hanson.entity.AcctbkSeqBean;
import com.hanson.entity.ApplyCashOutBean;
import com.hanson.entity.UserBean;
import com.hanson.mapper.AcctbkSeqBeanMapper;
import com.hanson.mapper.ApplyCashOutBeanMapper;
import com.hanson.mapper.UserBeanMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ApplyCashOutService {
    @Autowired
    private ApplyCashOutBeanMapper applyCashOutBeanMapper;
    @Autowired
    private UserBeanMapper userBeanMapper;
    @Autowired
    private AcctbkSeqBeanMapper acctbkSeqBeanMapper;

    public void add(ApplyCashOutBean applyCashOutBean, String openId) {
        int cashMoney = BusinessUtil.getDbAmount(applyCashOutBean.getWebMoney());
        if(cashMoney < 10000){
            //满100才能提现
            throw new BusinessException("提现金额不能少于100元");
        }
        if(cashMoney % 10000 != 0){
            throw new BusinessException("提现只能整百提现");
        }
        applyCashOutBean.setOpenId(openId);
        applyCashOutBean.setMoney(cashMoney);
        applyCashOutBean.setCreateDate(new Date());
        applyCashOutBean.setStatus(0);
        applyCashOutBeanMapper.insert(applyCashOutBean);
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(ApplyCashOutBean applyCashOutBean) {
        ApplyCashOutBean returnBean = applyCashOutBeanMapper.selectByPrimaryKey(applyCashOutBean.getId());
        if (null != returnBean) {
            returnBean.setUpdateDate(new Date());
            returnBean.setStatus(applyCashOutBean.getStatus());
            returnBean.setRemark(applyCashOutBean.getRemark());
            applyCashOutBeanMapper.updateByPrimaryKey(returnBean);
            //通过 扣钱
            if (returnBean.getStatus() == 1) {
                UserBean userBean = userBeanMapper.selectByOpenId(returnBean.getOpenId());
                if (null != userBean) {
                    if (userBean.getMoney() < returnBean.getMoney()) {
                        throw new BusinessException("账户余额不足");
                    } else {
                        userBean.setMoney(userBean.getMoney() - returnBean.getMoney());
                        userBeanMapper.updateByPrimaryKeySelective(userBean);
                        AcctbkSeqBean acctbkSeqBean = new AcctbkSeqBean();
                        acctbkSeqBean.setRemark("提现" + BusinessUtil.getWebAmount(returnBean.getMoney()) + "元");
                        acctbkSeqBean.setMoney(String.valueOf(returnBean.getMoney()));
                        acctbkSeqBean.setCreateDate(new Date());
                        acctbkSeqBean.setType(2);
                        acctbkSeqBean.setOpenId(returnBean.getOpenId());
                        acctbkSeqBeanMapper.insert(acctbkSeqBean);
                    }
                }
            }
        }
    }

    public void delete(Integer id) {
        applyCashOutBeanMapper.deleteByPrimaryKey(id);
    }

    public ApplyCashOutBean detail(Integer id) {
        ApplyCashOutBean applyCashOutBean = applyCashOutBeanMapper.selectByPrimaryKey(id);
        if (null != applyCashOutBean) {
            applyCashOutBean.setWebMoney(BusinessUtil.getWebAmount(applyCashOutBean.getMoney()));
        }
        return applyCashOutBean;
    }

    public Page<ApplyCashOutBean> listApplyCashOutBean(Page<ApplyCashOutBean> page, ApplyCashOutBean applyCashOutBean) {
        System.out.println(applyCashOutBean);
        List<ApplyCashOutBean> list = applyCashOutBeanMapper.listApplyCashOutBean(page, applyCashOutBean);
        if (CollectionUtils.isNotEmpty(list)) {
            list.stream().forEach(c -> {
                c.setWebMoney(BusinessUtil.getWebAmount(c.getMoney()));
            });
        }
        return page.setRecords(list);
    }

    public int statisDeal() {
        return applyCashOutBeanMapper.statisDeal();
    }
}
